import http.cookiejar, urllib.request

cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
res = opener.open('https://www.baidu.com/')
for p in cookie: 
    print(p.name + '=' + p.value)

filename = 'cookies.txt'
cookie = http.cookiejar.MozillaCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
res = opener.open('https://www.baidu.com/')
cookie.save(ignore_discard=True, ignore_expires=True)

cookie = http.cookiejar.MozillaCookieJar(filename)
cookie.load(ignore_discard=True, ignore_expires=True)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
res = opener.open('https://www.baidu.com/')
print(res.read().decode('utf-8'))